/*Copyright 2009 Stephan Bezoen, http://stephan.acidcats.nlLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at   	http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.*/package nl.acidcats.yalog.yala {	import nl.acidcats.yalog.yala.datafilter.SenderFilter;			import flash.text.TextField;			import fl.controls.CheckBox;
	import fl.controls.UIScrollBar;
	
	import nl.acidcats.yalog.common.Levels;
	import nl.acidcats.yalog.yala.datafilter.IDataFilter;
	import nl.acidcats.yalog.yala.datafilter.LevelFilter;
	import nl.acidcats.yalog.yala.ui.YalogViewer;
	import nl.acidcats.yalog.yala.ui.YalogViewerStatusEvent;
	import nl.acidcats.yalog.yala.ui.table.RowRenderer;
	
	import org.asaplibrary.ui.buttons.BaseButton;
	import org.asaplibrary.ui.buttons.HilightButton;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getQualifiedClassName;			/**	 * Application controller for the Yala application, to be used as document class for Yala.fla.	 */
	public class AppController extends MovieClip {		/** Height of the control bar at the bottome */		private static const CONTROLBAR_HEIGHT : Number = 30;				// stuff on the timeline		public var tBar : MovieClip;		public var tLed : MovieClip;		public var tPauseButton : BaseButton;		public var tClearButton : HilightButton;		public var tCopyButton : HilightButton;		public var tFilterButton : HilightButton;		public var tFilterText : TextField;		public var tScrollBar : UIScrollBar;
		public var tDebugCB : CheckBox;		public var tInfoCB : CheckBox;		public var tWarnCB : CheckBox;				// message filters		private var mDebugMessageFilter : IDataFilter = new LevelFilter(Levels.DEBUG);		private var mInfoMessageFilter : IDataFilter = new LevelFilter(Levels.INFO);		private var mWarnMessageFilter : IDataFilter = new LevelFilter(Levels.WARN);		private var mSenderFilter : SenderFilter = new SenderFilter();		// the actual message viewer		private var mYalogViewer : YalogViewer;		private var mHasSenderFilter : Boolean;				/**		 * Constructor		 */		public function AppController () {			initUI();		}
		/**		 * Initialize user interface elements		 */		private function initUI () : void {			// no scaling			stage.scaleMode = StageScaleMode.NO_SCALE;			stage.align = StageAlign.TOP_LEFT;						// listen to resizing events			stage.addEventListener(Event.RESIZE, handleStageResize);						// create container for Yalog viewer			var s : Sprite = addChildAt(new Sprite(), getChildIndex(tScrollBar) - 1) as Sprite;						// create Yalog viewer			mYalogViewer = new YalogViewer(s, stage.stageWidth, tBar.y);			mYalogViewer.addEventListener(YalogViewerStatusEvent._EVENT, handleViewerStatusEvent);			mYalogViewer.setScrollBar(tScrollBar);			mYalogViewer.connect();						// initialize positioning			handleStageResize();						// handle click events			tClearButton.addEventListener(MouseEvent.CLICK, handleClearClick);			tCopyButton.addEventListener(MouseEvent.CLICK, handleCopyClick);			tPauseButton.addEventListener(MouseEvent.CLICK, handlePauseClick);			tFilterButton.addEventListener(MouseEvent.CLICK, handleFilterClick);			tDebugCB.addEventListener(MouseEvent.CLICK, handleLevelCheckboxClick);			tInfoCB.addEventListener(MouseEvent.CLICK, handleLevelCheckboxClick);			tWarnCB.addEventListener(MouseEvent.CLICK, handleLevelCheckboxClick);						// style checkboxes			styleCheckbox(tDebugCB, RowRenderer.COLORS[0], "Debug");
			styleCheckbox(tInfoCB, RowRenderer.COLORS[1], "Info");
			styleCheckbox(tWarnCB, RowRenderer.COLORS[3], "Warn");		}				private function handleFilterClick (e : MouseEvent) : void {			if (tFilterText.text == "") {				if (mHasSenderFilter) {					mHasSenderFilter = false;					mYalogViewer.removeFilter(mSenderFilter);				}			} else {				mSenderFilter.sender = tFilterText.text;				if (!mHasSenderFilter) {					mHasSenderFilter = true;					mYalogViewer.addFilter(mSenderFilter);				} else {					mYalogViewer.update();				}			}		}		private function handlePauseClick (e : MouseEvent) : void {			mYalogViewer.pause(); 		}		private function handleCopyClick (e : MouseEvent) : void {			mYalogViewer.copyDataToClipboard(); 		}		private function handleClearClick (e : MouseEvent) : void {			mYalogViewer.clear();		}		/**		 * Set styling properties of specified checkbox		 * @param inCheckbox: checkbox to style		 * @param inColor: text color of label		 * @param inText: label text		 */		private function styleCheckbox (inCheckbox : CheckBox, inColor : uint, inText : String) : void {			var tf:TextFormat = new TextFormat();			tf.font = "Arial";			tf.size = 12;			tf.bold = true;			tf.color = inColor;						inCheckbox.textField.autoSize = TextFieldAutoSize.LEFT;			inCheckbox.setStyle("textFormat", tf);						inCheckbox.label = inText;		}		/**		 * Handle status events from YalogViewer		 */				private function handleViewerStatusEvent (e : YalogViewerStatusEvent) : void {			switch (e.subtype) {				case YalogViewerStatusEvent.ACTIVE: tLed.gotoAndStop("activity"); break;				case YalogViewerStatusEvent.INACTIVE: tLed.gotoAndStop("connected"); break;				case YalogViewerStatusEvent.ERROR: tLed.gotoAndStop("error"); break;				case YalogViewerStatusEvent.PAUSED: tLed.gotoAndStop("pause"); break;			}		}		/**		 *	Handle event from stage that it has been resized		 */		private function handleStageResize (e : Event = null) : void {			var sh : Number = stage.stageHeight;						// ignore zero-height event, sometimes fired when used as Eclipse plugin			if (sh == 0) return;						// pass on size to viewer			mYalogViewer.setSize(stage.stageWidth, sh - CONTROLBAR_HEIGHT);						// reposition and resize some other interface elements			tBar.width = stage.stageWidth;			tBar.y = sh - CONTROLBAR_HEIGHT;						tClearButton.y = sh - tClearButton.height + 2;			tCopyButton.y = sh - tCopyButton.height - 2;			tPauseButton.y = sh - tPauseButton.height + 4;
			tLed.y = sh - tLed.height + 2;			tDebugCB.y = sh - (tDebugCB.height + 2);			tInfoCB.y = sh - (tDebugCB.height + 2);
			tWarnCB.y = sh - (tDebugCB.height + 2);			tFilterButton.y = sh - tFilterButton.height + 4;			tFilterText.y = sh - tFilterText.height - 4;		}		/**		 *	CheckBox handler for level checkboxes		 */		private function handleLevelCheckboxClick (e : MouseEvent) : void {			var cb : CheckBox = e.target as CheckBox;						if (cb.selected) { // select filter to remove				switch (cb) {					case tDebugCB: mYalogViewer.removeFilter(mDebugMessageFilter); break;					case tInfoCB: mYalogViewer.removeFilter(mInfoMessageFilter); break;					case tWarnCB: mYalogViewer.removeFilter(mWarnMessageFilter); break;				}			} else { // select filter to add				switch (cb) {					case tDebugCB: mYalogViewer.addFilter(mDebugMessageFilter); break;					case tInfoCB: mYalogViewer.addFilter(mInfoMessageFilter); break;					case tWarnCB: mYalogViewer.addFilter(mWarnMessageFilter); break;				}			}		}		override public function toString():String {			return getQualifiedClassName(this);		}	}}
